# 导入
import requests

# 进行登陆
response = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
                         headers={"Content-Type": "application/json"},
                         json={"mobile": "13800000002", "password": "123456"})

# 打印登陆的结果
print(response.json())

# 提取令牌
token = "Bearer " + response.json().get("data")
print("提取到的令牌为：", token)

import app

print("app.HEADERS没有设置令牌之前的结果为：", app.HEADERS)
app.HEADERS['Authorization'] = token  # 把令牌放在全局变量app.py下的HEADERS中
print("app.HEADERS的结果为", app.HEADERS)
# 从第17行开始，后续所有的代码，都可以使用app.HEADERS中的令牌

# 实现添加员工
add_emp_url = "http://ihrm-test.itheima.net/api/sys/user"
response = requests.post(url=add_emp_url, headers=app.HEADERS,
                         json={"username": "比卡丘",
                               "mobile": "173121432439",
                               "timeOfEntry": "2020-08-04",
                               "formOfEmployment": 1,
                               "departmentName": "111",
                               "correctionTime": "2020-08-09T16:00:00.000Z"})
print("添加员工的结果为:", response.json())
# 提取员工ID并保存到全局变量
print("保存员工ID之前app.EMPID", app.EMP_ID)
app.EMP_ID = response.json().get("data").get("id")
print("保存员工ID之后app.EMPID", app.EMP_ID)

# 实现修改员工
modify_url = "http://ihrm-test.itheima.net/api/sys/user" + "/" + app.EMP_ID
print("修改员工的URL为：", modify_url)
response = requests.put(url=modify_url,
                        headers=app.HEADERS,
                        json={"username":"葫芦娃"})
print("修改员工的结果为：", response.json())

# 查询
query_url = "http://ihrm-test.itheima.net/api/sys/user" + "/" + app.EMP_ID
print("查询员工的URL为：", query_url)
response = requests.get(url=query_url, headers=app.HEADERS)
print("查询员工的结果为：", response.json())

# 实现删除员工
delete_url = "http://ihrm-test.itheima.net/api/sys/user" + "/" + app.EMP_ID
print("删除员工的URL：", delete_url)
response = requests.delete(url=delete_url, headers=app.HEADERS)
print("删除员工的结果为：", response.json())
